package handler

import (
	"github.com/gin-gonic/gin"
	"platform-sys/model"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	if title == "" || content == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "不能为空",
		})
		return
	}

	tx := model.DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		tx.Rollback()
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "评论失败2",
		})
		return

	}
	data := model.Article{
		Title:   title,
		Content: content,
	}

	if err := model.DB.Create(&data).Error; err != nil {
		tx.Rollback()
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "评论失败",
		})
		return
	}
	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "评论失败3",
		})
		return

	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "评论成功",
	})
	return
}
func List(c *gin.Context) {
	var art []model.Article
	model.DB.Order("id desc").Find(&art)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": art,
	})
	return
}
